package com.hermes.biz.service.user.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.hermes.biz.common.model.base.EventBaseDO;
import com.hermes.biz.common.model.message.MessageDO;
import com.hermes.biz.common.model.message.MessageInfoDO;
import com.hermes.biz.common.model.message.PostMessageUnitDO;
import com.hermes.biz.common.model.user.UserCommentDO;
import com.hermes.biz.common.model.user.UserMessageDO;
import com.hermes.biz.common.util.message.UserEventRepoUtil;
import com.hermes.biz.manager.message.MessageManager;
import com.hermes.biz.manager.user.UserMessageManager;
import com.hermes.biz.manager.usermodelextra.UserModelExtraManager;
import com.hermes.biz.service.user.UserMessageService;

/**
 *
 */
public class UserMessageServiceImpl implements UserMessageService {

    private MessageManager        messageManager;

    private UserMessageManager    userMessageManager;

    private UserModelExtraManager userModelExtraManager;

    public long getUserNewMessageByType(long userId, int type) {

        long count = this.userMessageManager.getUserNewMessageByType(userId, type);

        if (count > 0) {
            return count;
        }
        return 0;
    }

    public List<MessageInfoDO> getUserNewMessageByUserId(long userId) {

        List<UserMessageDO> userMessageList = this.userMessageManager
            .getUserNewMessageByUserId(userId);

        if (userMessageList != null && userMessageList.size() > 0) {
            List<MessageInfoDO> resultList = new ArrayList<MessageInfoDO>();
            for (UserMessageDO userMessageDO : userMessageList) {

                MessageDO messageDO = this.messageManager.getMessageContenById(userMessageDO
                    .getMessageId());
                MessageInfoDO messageInfoDO = new MessageInfoDO();
                messageInfoDO.setItemId(userMessageDO.getItemId());
                messageInfoDO.setMessageId(userMessageDO.getMessageId());
                messageInfoDO.setMessageContent(messageDO.getMessageContent());
                messageInfoDO.setMessageType(userMessageDO.getMessageType());
                messageInfoDO.setGmtCreate(messageDO.getGmtCreate());

                resultList.add(messageInfoDO);
            }
        }

        return new ArrayList<MessageInfoDO>();
    }

    public long getUserMessageCountByUserId(long userId) {
        return this.userMessageManager.getUserMessageCountByUserId(userId);
    }

    public List<MessageInfoDO> getUserMessageByType(long userId, int type) {

        List<UserMessageDO> userMessageList = this.userMessageManager.getUserMessageByType(userId,
            type);

        if (userMessageList != null && userMessageList.size() > 0) {
            List<MessageInfoDO> resultList = new ArrayList<MessageInfoDO>();
            for (UserMessageDO userMessageDO : userMessageList) {
                MessageDO messageDO = this.messageManager.getMessageContenById(userMessageDO
                    .getMessageId());
                MessageInfoDO messageInfoDO = new MessageInfoDO();
                messageInfoDO.setItemId(userMessageDO.getItemId());
                messageInfoDO.setUserId(messageDO.getCreateUserId());
                messageInfoDO.setMessageId(userMessageDO.getMessageId());
                messageInfoDO.setMessageContent(messageDO.getMessageContent());
                messageInfoDO.setMessageType(userMessageDO.getMessageType());
                messageInfoDO.setGmtCreate(messageDO.getGmtCreate());

                resultList.add(messageInfoDO);
            }
        }

        return new ArrayList<MessageInfoDO>();
    }

    /**
     * Save the Message Event 
     * 
     */
    public boolean saveMessageEventInfo(EventBaseDO event) {

        if (event instanceof UserCommentDO) {

            UserCommentDO userCommentDO = (UserCommentDO) event;

            MessageDO messageDO = new MessageDO();

            messageDO.setGmtCreate(new Date());
            messageDO.setGmtModified(new Date());
            messageDO.setItemId(userCommentDO.getItemId());
            messageDO.setMessageContent(userCommentDO.getComment());
            messageDO.setStatus(0);
            //messageDO.setMessageId(123456);
            messageDO.setCreateUserId(userCommentDO.getCreateUserId());

            long messageId = this.messageManager.saveMessageInfo(messageDO);

            List<Long> userList = userModelExtraManager.getUserFansList(userCommentDO
                .getCreateUserId());
            if (userList != null && userList.size() > 0) {
                UserEventRepoUtil eventRepo = UserEventRepoUtil.getUserEventMapUtil();
                for (long userIdNum : userList) {
                    UserMessageDO userMessageDO = new UserMessageDO();
                    userMessageDO.setItemId(userCommentDO.getItemId());
                    userMessageDO.setMessageId(messageId);
                    userMessageDO.setMessageType(1);
                    userMessageDO.setRead(0);
                    userMessageDO.setUserId(userIdNum);
                    userMessageDO.setStatus(0);
                    this.userMessageManager.saveUserMessageInfo(userMessageDO);

                    if (eventRepo.getIdNumInMap(userIdNum) != null) {
                        PostMessageUnitDO currentUnit = eventRepo.getIdNumInMap(userIdNum);
                        int ccount = currentUnit.getComment();
                        ccount += 1;
                        currentUnit.setComment(ccount);
                        eventRepo.putUserInfoToMap(userIdNum, currentUnit);
                    } else {
                        PostMessageUnitDO postUnit = new PostMessageUnitDO();
                        int ccount = 1;
                        postUnit.setComment(ccount);
                        eventRepo.putUserInfoToMap(userIdNum, postUnit);
                    }
                }
            }
        }

        return false;
    }

    public MessageManager getMessageManager() {
        return messageManager;
    }

    public void setMessageManager(MessageManager messageManager) {
        this.messageManager = messageManager;
    }

    public UserMessageManager getUserMessageManager() {
        return userMessageManager;
    }

    public void setUserMessageManager(UserMessageManager userMessageManager) {
        this.userMessageManager = userMessageManager;
    }

    public UserModelExtraManager getUserModelExtraManager() {
        return userModelExtraManager;
    }

    public void setUserModelExtraManager(UserModelExtraManager userModelExtraManager) {
        this.userModelExtraManager = userModelExtraManager;
    }

}
